*****************************************************************************
* .do-file to combine all single family year files of PSID (waves 1989-2017)
*****************************************************************************

clear all
set maxvar 7000


/////////////////////////////////////////////////////////////
////////// prepare raw data with .do files from PSID ////////
/////////////////////////////////////////////////////////////

// !!!!! path needs to be adjustes also within these do-files !!!!!

// family files 
foreach num of numlist 1997(2)2017 {
cd "$psidrawdata/fam`num'er"
do FAM`num'ER
}


foreach num of numlist 1989/1996 {
cd "$psidrawdata/fam`num'er"
do FAM`num'ER
}

foreach num of numlist 1984 {
cd "$psidrawdata/fam`num'er"
do FAM`num'ER
}

// wealth files
foreach num of numlist 1999(2)2007 {
cd "$psidrawdata/wlth`num'"
do wlth`num'
}

foreach num of numlist 1984(5)1994 {
cd "$psidrawdata/wlth`num'"
do wlth`num'
}


////////////////////////////////////////////////////////////////////
/////////////////ADJUST EACH WAVE INDIVIDUALLY//////////////////////
////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
//////////////////////// wave 2017 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/file2017.dta", clear 


rename ER66009 famid68			// 1968 family identifier
rename ER71560 hhid				// household id number 
rename ER66002 famid			// 2017 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER71570 famweight 			// core family weight (longitudinal)


// demographic variables 
ren ER66016 hhmemb  			// number of members in family unit

ren ER66017 ageref				// age of reference person
ren ER66019 agesp				// age of spouse

ren ER66018 sexref				// sex of reference person
ren ER66020 sexsp				// sex of spouse

ren ER66021 numchild			// number of children in family unit

ren ER66024 martref				// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER71540 martgen				// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER71538 educref				// completed years of education (coded as actual number), reference person
ren ER71539 educsp				// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren ER71481 homequ				// home equity (value of home minus mortgage) 
ren ER71483 wealthwoequity		// wealth without equity (sum of all assets net of debt)	
ren ER71485 wealth				// wealth including equity (sum of all assets net of debt, plus home equity)
ren ER71443 stocks				// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren ER71445 amountstock			// amount of stock holdings
ren ER71433 savings				// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren ER71435 amtsavings			// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus real estate value plus real estate debt
gen finwealth = wealthwoequity - ER71439 + ER71441


// income variables 
*total 
ren ER71426 totfaminc			//  total family income 

*other family members
ren ER71398 inc_taxable_oth		// taxable income of other members in family unit
ren ER71396 inc_asset_oth		// total asset income of other members in family unit
ren ER71394 inc_labor_oth		// total labor income of other members in family unit
ren ER71419 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER71424 inc_ss_oth			// total social security income of other members in family unit 

*reference person + spouse
ren ER71330 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER71391 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person
ren ER71420 inc_ss_ref 			// reference person, social security income 
ren ER71294 inc_rent_ref		// reference person, rental income
ren ER71296 inc_div_ref			// reference person, income from dividends
ren ER71298 inc_interest_ref	// reference person, inocme from interest
ren ER71300 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER71274 inc_laborbus_ref    // reference person, labor income from business
ren ER71293 inc_labor_ref		// reference person, labor income (excluding farm and business)

*spouse
ren ER71422 inc_ss_sp 			// spouse, social security income
ren ER71322 inc_rent_sp			// spouse, rental income 
ren ER71324 inc_div_sp			// spouse, income from dividends
ren ER71326 inc_interest_sp		// spouse, income from interest
ren ER71328 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER71321 inc_labor_sp		// spouse, labor income (excluding farm and business)
ren ER71302 inc_laborbus_sp		// spouse, labor income from business 


// further variables adjustments
gen wave = 2017

drop ER*

save "$psidwkddata/file2017adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2015 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/file2015.dta", clear

rename ER60009 famid68 		// 1968 family identifier
rename ER65481 hhid			// household id number 
rename ER60002 famid		// 2015 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER65492 famweight 		// core family weight (longitudinal)

// demographic variables
ren ER60016 hhmemb			// number of members in family unit 
	
ren ER60017 ageref			// age of reference person
ren ER60019 agesp			// age of spouse

ren ER60018 sexref			// sex of reference person
ren ER60020 sexsp			// sex of spouse

ren ER60021 numchild		// number of children in family unit

ren ER60024 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER65461 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER65459 educref			// completed years of education (coded as actual number), reference person
ren ER65460 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren ER65404 homequ			// home equity (value of home minus mortgage) 
ren ER65406 wealthwoequity	// wealth without equity (sum of all assets net of debt)	
ren ER65408 wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren ER65366 stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren ER65368 amountstock		// amount of stock holdings
ren ER65356 savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren ER65358 amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus real estate value plus real estate debt
gen finwealth = wealthwoequity - ER65362 + ER65364 

// income variables 
*total 
ren ER65349 totfaminc			//  total family income

*other family members
ren ER65321 inc_taxable_oth		// taxable income of other members in family unit
ren ER65319 inc_asset_oth		// total asset income of other members in family unit
ren ER65317 inc_labor_oth		// total labor income of other members in family unit
ren ER65342 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER65347 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren ER65253 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER65314 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person 
ren ER65343 inc_ss_ref 			// reference person, social security income 
ren ER65217 inc_rent_ref		// reference person, rental income
ren ER65219 inc_div_ref			// reference person, income from dividends
ren ER65221 inc_interest_ref	// reference person, inocme from interest
ren ER65223 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER65197 inc_laborbus_ref	// reference person, labor income from business
ren ER65216 inc_labor_ref		// reference person, labor income (excluding farm and business)

*spouse
ren ER65345 inc_ss_sp 			// spouse, social security income
ren ER65245 inc_rent_sp			// spouse, rental income 
ren ER65247 inc_div_sp			// spouse, income from dividends
ren ER65249 inc_interest_sp		// spouse, income from interest
ren ER65251 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER65225 inc_laborbus_sp		// spouse, labor income from business
ren ER65244 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
gen wave = 2015

drop ER*

save "$psidwkddata/file2015adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2013 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/file2013.dta", clear

rename ER53009 famid68		// 1968 family identifier
rename ER58245 hhid			// household id number
rename ER53002 famid		// 2013 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER58257 famweight 		// core family weight (longitudinal)

// demographic variables
ren ER53016 hhmemb			// number of members in family unit

ren ER53017 ageref			// age of reference person
ren ER53019 agesp			// age of spouse

ren ER53018 sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER53020 numchild		// number of children in family unit

ren ER53023 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER58225 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER58223 educref			// completed years of education (coded as actual number), reference person
ren ER58224 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren ER58207 homequ			// home equity (value of home minus mortgage) 
ren ER58209 wealthwoequity	// wealth without equity (sum of all assets net of debt)
ren ER58211 wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren ER58169 stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren ER58171 amountstock		// amount of stock holdings
ren ER58159 savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren ER58161 amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus real estate value plus real estate debt
gen finwealth = wealthwoequity - ER58165 + ER58167

// income variables 
*total 
ren ER58152 totfaminc		//  total family income

*other family members
ren ER58124 inc_taxable_oth	// taxable income of other members in family unit
ren ER58122 inc_asset_oth	// total asset income of other members in family unit
ren ER58120 inc_labor_oth	// total labor income of other members in family unit
ren ER58145 inc_transf_oth	// total transfer income of other members in family unit (except social security)
ren ER58150 inc_ss_oth		// total social security income of other members in family unit 

*reference person + spouse
ren ER58060 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER58117 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person
ren ER58146 inc_ss_ref 			// reference person, social security income 
ren ER58039 inc_rent_ref		// reference person, rental income
ren ER58041 inc_div_ref			// reference person, income from dividends
ren ER58043 inc_interest_ref	// reference person, inocme from interest
ren ER58045 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER58038 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER58017 inc_laborbus_ref	// reference person, labor income from business

*spouse
ren ER58148 inc_ss_sp 			// spouse, social security income
ren ER58052 inc_rent_sp			// spouse, rental income
ren ER58054 inc_div_sp			// spouse, income from dividends
ren ER58056 inc_interest_sp		// spouse, income from interest
ren ER58058 inc_trustfund_sp	// spouse, income from trusts/rolyalties	
ren ER58047 inc_laborbus_sp		// spouse, labor income from business
ren ER58050 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
gen wave = 2013

drop ER*

save "$psidwkddata/file2013adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2011 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/file2011.dta", clear 

rename ER47309 famid68		// 1968 family identifier
rename ER52427 hhid			// household id number
rename ER47302 famid		// 2011 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER52436 famweight 	// core family weight (longitudinal)

// demographic variables 
ren ER47316 hhmemb		// number of members in family unit

ren ER47317 ageref		// age of reference person
ren ER47319 agesp		// age of spouse

ren ER47318 sexref		// sex of reference person
gen sexsp = . 			// before 2015: male was automatically head, spouse female

ren ER47320 numchild	// number of children in family unit

ren ER47323 martref		// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER52407 martgen		// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER52405 educref		// completed years of education (coded as actual number), reference person
ren ER52406 educsp		// completed years of education (coded as actual number), spouse


// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren ER52390 homequ				// home equity (value of home minus mortgage)
ren ER52392 wealthwoequity		// wealth without equity (sum of all assets net of debt)
ren ER52394 wealth				// wealth including equity (sum of all assets net of debt, plus home equity)
ren ER52356 stocks				// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren ER52358 amountstock			// amount of stock holdings
ren ER52348 savings				// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren ER52350 amtsavings			// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - ER52354

// income variables 
*total 
ren ER52343 totfaminc			// total family income 

*other family members
ren ER52315 inc_taxable_oth		// taxable income of other members in family unit
ren ER52313 inc_asset_oth		// total asset income of other members in family unit
ren ER52311 inc_labor_oth		// total labor income of other members in family unit
ren ER52336 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER52341 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren ER52259 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER52308 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person
ren ER52337 inc_ss_ref 			// reference person, social security income
ren ER52238 inc_rent_ref		// reference person, rental income
ren ER52240 inc_div_ref			// reference person, income from dividends
ren ER52242 inc_interest_ref	// reference person, inocme from interest	
ren ER52244 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER52216 inc_laborbus_ref	// reference person, labor income from business	
ren ER52237 inc_labor_ref		// reference person, labor income (excluding farm and business)

*spouse
ren ER52339 inc_ss_sp 			// spouse, social security income
ren ER52251 inc_rent_sp			// spouse, rental income 
ren ER52253 inc_div_sp			// spouse, income from dividends
ren ER52255 inc_interest_sp		// spouse, income from interest
ren ER52257 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER52246 inc_laborbus_sp		// spouse, labor income from business
ren ER52249 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
gen wave = 2011

drop ER*

save "$psidwkddata/file2011adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2009 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/file2009.dta", clear 


rename ER42009 famid68		// 1968 family identifier
rename ER47003 hhid			// household id number
rename ER42002 famid		// 2009 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER47012 famweight 		// core family weight (longitudinal)

// demographic variables 
ren ER42016 hhmemb			// number of members in family unit

ren ER42017 ageref			// age of reference person
ren ER42019 agesp			// age of spouse 
ren ER42018 sexref			// sex of reference person 
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER42020 numchild		// number of children in family unit 

ren ER42023 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER46983 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)
	
ren ER46981 educref			// completed years of education (coded as actual number), reference person
ren ER46982 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren ER46966 homequ			// home equity (value of home minus mortgage) 
ren ER46968 wealthwoequity	// wealth without equity (sum of all assets net of debt)	
ren ER46970 wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren ER46952 stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren ER46954 amountstock		// amount of stock holdings 
ren ER46940 savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren ER46942 amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)	

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - ER46950


// income variables
*total 
ren ER46935 totfaminc			//  total family income

*other family members
ren ER46907 inc_taxable_oth		// taxable income of other members in family unit 
ren ER46905 inc_asset_oth		// total asset income of other members in family unit
ren ER46903 inc_labor_oth		// total asset income of other members in family unit
ren ER46928 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER46933 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren ER46851 inc_taxable_refsp   // taxable income of reference person and spouse
ren ER46900 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person
ren ER46929 inc_ss_ref 			// reference person, social security income 
ren ER46830 inc_rent_ref		// reference person, rental income
ren ER46832 inc_div_ref			// reference person, income from dividends
ren ER46834 inc_interest_ref	// reference person, inocme from interest
ren ER46836 inc_trustfund_ref   // reference person, income from trusts/rolyalties
ren ER46829 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER46808 inc_laborbus_ref	// reference person, labor income from business

*spouse
ren ER46931 inc_ss_sp 			// spouse, social security income
ren ER46843 inc_rent_sp			// spouse, rental income 
ren ER46845 inc_div_sp			// spouse, income from dividends
ren ER46847 inc_interest_sp		// spouse, income from interest
ren ER46849 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER46838 inc_laborbus_sp		// spouse, labor income from business
ren ER46841 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
gen wave = 2009

drop ER*

save "$psidwkddata/file2009adj.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 2007 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth2007.dta", clear
ren S801 famid
save "$psidwkddata/wealth2007.dta", replace
restore


use "$psidwkddata/file2007.dta", clear

rename ER36009 famid68		// 1968 family identifier
rename ER41059 hhid			// household id number
rename ER36002 famid		// 2007 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER41069 famweight 		// core family weight (longitudinal)


// merge wealth with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth2007.dta"
drop _merge // all matched

// demographic variables 
ren ER36016 hhmemb			// number of members in family unit

ren ER36017 ageref			// age of reference person
ren ER36019 agesp			// age of spouse

ren ER36018 sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER36020 numchild		// number of children in family unit

ren ER36023 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER41039 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER41037 educref			// completed years of education (coded as actual number), reference person
ren ER41038 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S820	homequ			// home equity (value of home minus mortgage) 
ren S816	wealthwoequity	// wealth without equity (sum of all assets net of debt)
ren S817	wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren S810	stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S811	amountstock		// amount of stock holdings
ren S804 	savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S805	amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S809


// income variables 
*total 
ren ER41027 totfaminc			//  total family income 

*other family members
ren ER40999 inc_taxable_oth		// taxable income of other members in family unit
ren ER40997 inc_asset_oth		// total asset income of other members in family unit
ren ER40995 inc_labor_oth		// total labor income of other members in family unit
ren ER41020 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER41025 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren ER40943 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER40992 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person
ren ER41021 inc_ss_ref 			// reference person, social security income
ren ER40922 inc_rent_ref		// reference person, rental income
ren ER40924 inc_div_ref			// reference person, income from dividends
ren ER40926 inc_interest_ref	// reference person, inocme from interest
ren ER40928 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER40900 inc_laborbus_ref	// reference person, labor income from business
ren ER40921 inc_labor_ref		// reference person, labor income (excluding farm and business)


*spouse
ren ER41023 inc_ss_sp 			// spouse, social security income
ren ER40935 inc_rent_sp			// spouse, rental income
ren ER40937 inc_div_sp			// spouse, income from dividends
ren ER40939 inc_interest_sp		// spouse, income from interest
ren ER40941 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER40930 inc_laborbus_sp		// spouse, labor income from business
ren ER40933 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
drop S8* ER*

gen wave = 2007

save "$psidwkddata/file2007adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2005 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth2005.dta", clear
ren S701 famid
save "$psidwkddata/wealth2005.dta", replace
restore

use "$psidwkddata/file2005.dta", clear 

rename ER25009 famid68		// 1968 family identifier	
rename ER28069 hhid			// household id number
rename ER25002 famid		// 2005 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER28078 famweight 		// core family weight (longitudinal)


// merge wealth with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth2005.dta"
drop _merge // all matched

// demographic variables
ren ER25016 hhmemb			// number of members in family unit

ren ER25017 ageref			// age of reference person
ren ER25019 agesp			// age of spouse 

ren ER25018 sexref			// sex of reference person 
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER25020 numchild		// number of children in family unit

ren ER25023 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER28049 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER28047 educref			// completed years of education (coded as actual number), reference person
ren ER28048 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S720	homequ				// home equity (value of home minus mortgage)
ren S716	wealthwoequity		// wealth without equity (sum of all assets net of debt)
ren S717	wealth				// wealth including equity (sum of all assets net of debt, plus home equity)
ren S710	stocks				// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S711	amountstock			// amount of stock holdings
ren S704 	savings				// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S705	amtsavings			// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S709


// income variables
*total 
ren ER28037 totfaminc			//  total family income

*other family members
ren ER28009 inc_taxable_oth		// taxable income of other members in family unit
ren ER28007 inc_asset_oth		// total asset income of other members in family unit
ren ER28005 inc_labor_oth		// total labor income of other members in family unit
ren ER28030 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren ER28035 inc_ss_oth			// total social security income of other members in family unit 

*respondent + spouse
ren ER27953 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER28002 inc_transf_refsp	// tranfer income of reference person and spouse

*respondent
ren ER28031 inc_ss_ref 			// reference person, social security income 
ren ER27932 inc_rent_ref		// reference person, rental income
ren ER27934 inc_div_ref			// reference person, income from dividends
ren ER27936 inc_interest_ref	// reference person, inocme from interest
ren ER27938 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren ER27910 inc_laborbus_ref	// reference person, labor income from business
ren ER27931  inc_labor_ref		// reference person, labor income (excluding farm and business)

*spouse
ren ER28033 inc_ss_sp 			// spouse, social security income
ren ER27945 inc_rent_sp			// spouse, rental income 
ren ER27947 inc_div_sp			// spouse, income from dividends
ren ER27949 inc_interest_sp		// spouse, income from interest
ren ER27951 inc_trustfund_sp	// spouse, income from trusts/rolyalties
ren ER27940 inc_laborbus_sp		// spouse, labor income from business 
ren ER27943 inc_labor_sp		// spouse, labor income (excluding farm and business)


// further variables adjustments
drop S7* ER*

gen wave = 2005

save "$psidwkddata/file2005adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2003 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth2003.dta", clear
ren S601 famid
save "$psidwkddata/wealth2003.dta", replace
restore

use "$psidwkddata/file2003.dta", clear 

rename ER21009  famid68		// 1968 family identifier
rename ER24170  hhid		// household id number 
rename ER21002  famid		// 2005 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER24179 famweight 		// core family weight (longitudinal)

// merge with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth2003.dta"
drop _merge // all matched

// demographic variables
ren ER21016 hhmemb			// number of members in family unit

ren ER21017 ageref			// age of reference person
ren ER21019 agesp			// age of spouse 

ren ER21018	sexref			// sex of reference person 
gen sexsp = . 			    // before 2015: male was automatically head, spouse female

ren ER21020 numchild		// number of children in family unit 

ren ER21023 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER24150 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)
	
ren ER24148 educref			// completed years of education (coded as actual number), reference person
ren ER24149 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S620	homequ			// home equity (value of home minus mortgage) 
ren S616	wealthwoequity	// wealth without equity (sum of all assets net of debt)
ren S617	wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren S610	stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S611	amountstock		// amount of stock holdings
ren S604 	savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S605	amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S609


// income variables 
*total 
ren ER24099 totfaminc			//  total family income
ren ER24104 inc_ss_all			//  total family income from social security

*other family members
ren ER24102 inc_taxable_oth		// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER24103 inc_transf_oth		// total transfer income of other members in family unit (except social security)	
gen inc_ss_oth = .				// total social security income of other members in family unit 

*reference person + spouse
ren ER24100 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER24101 inc_transf_refsp	// tranfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income 
gen inc_rent_ref = .			// reference person, rental income				
gen inc_div_ref = .				// reference person, income from dividends	
gen inc_interest_ref = .		// reference person, inocme from interest	
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties

ren ER24116 inc_labor_ref  		// reference person, labor income (excluding farm and business)     
ren ER24109 inc_laborbus_ref	// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income	
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest	
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER24135 inc_labor_sp		// spouse, labor income (excluding farm and business)
ren ER24111 inc_laborbus_sp		// spouse, labor income from business


// further variables adjustments
drop S6* ER*

gen wave = 2003

save "$psidwkddata/file2003adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2001 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth2001.dta", clear
ren S501 famid
save "$psidwkddata/wealth2001.dta", replace
restore

use "$psidwkddata/file2001.dta", clear 

rename ER17022 famid68		// 1968 family identifier
rename ER20393 hhid			// household id number 
rename ER17002 famid		// 2001 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER20394 famweight 		// core family weight (longitudinal)

*merge with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth2001.dta"
drop _merge // all matched

ren ER17012 hhmemb			// number of members in family unit

ren ER17013 ageref			// age of reference person
ren ER17015 agesp			// age of spouse 

ren ER17014	sexref			// sex of reference person
gen sexsp = .  				// before 2015: male was automatically head, spouse female

ren ER17016 numchild		// number of children in family unit

ren ER17024 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER20369 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER20457 educref			// completed years of education (coded as actual number), reference person
ren ER20458 educsp			// completed years of education (coded as actual number), spouse


// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S520	homequ			// home equity (value of home minus mortgage) 
ren S516	wealthwoequity	// wealth without equity (sum of all assets net of debt)
ren S517	wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren S510	stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S511	amountstock		// amount of stock holdings
ren S504 	savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S505	amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S509

// income variables 
*total 
ren ER20456 totfaminc		//  total family income
ren ER20455 inc_ss_all		//  total family income from social security

*other family members
ren ER20453 inc_taxable_oth	 	// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER20454 inc_transf_oth		// total transfer income of other members in family unit (except social security)
gen inc_ss_oth = .				// total social security income of other members in family unit

*reference person   + spouse
ren ER20449 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER20450 inc_transf_refsp	// transfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income 
gen inc_rent_ref = .			// reference person, rental income 
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, income from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren ER20443 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER20422 inc_laborbus_ref  	// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER20447 inc_labor_sp		// spouse, labor income (excluding farm and business)
ren ER20444 inc_laborbus_sp		// spouse, labor income from business 


// further variables adjustments
drop S5* ER*

gen wave = 2001

save "$psidwkddata/file2001adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1999 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth1999.dta", clear
ren S401 famid
save "$psidwkddata/wealth1999.dta", replace
restore

use "$psidwkddata/file1999.dta", clear 

rename ER13019 famid68			// 1968 family identifier
rename ER16447 hhid				// household id number
rename ER13002 famid			// 1999 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER16518 famweight 			// core family weight (longitudinal)


// merge with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth1999.dta"
drop _merge // all matched

// demographic variables
ren ER13009 hhmemb				// number of members in family unit

ren ER13010 ageref				// age of reference person
ren ER13012 agesp				// age of spouse

ren ER13011	sexref				// sex of reference person
gen sexsp = . 					// before 2015: male was automatically head, spouse female

ren ER13013 numchild			// number of children in family unit

ren ER13021 martref				// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER16423 martgen				// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER16516 educref				// completed years of education (coded as actual number), reference person
ren ER16517 educsp				// completed years of education (coded as actual number), spouse


// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S420	homequ				// home equity (value of home minus mortgage) 
ren S416	wealthwoequity		// wealth without equity (sum of all assets net of debt)	
ren S417	wealth				// wealth including equity (sum of all assets net of debt, plus home equity)
ren S410	stocks				// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S411	amountstock			// amount of stock holdings
ren S404 	savings				// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S405	amtsavings			// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S409

// income variables 
*total 
ren ER16462 totfaminc			//  total family income 
ren ER16460 inc_ss_all			//  total family income from social security

*other family members
ren ER16456 inc_taxable_oth		// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER16458 inc_transf_oth		// total transfer income of other members in family unit
gen inc_ss_oth = .				// total social security income of other members in family unit (except social security)

*reference person + spouse
ren ER16452 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER16454 inc_transf_refsp	// transfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income
gen inc_rent_ref = .			// reference person, rental income
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren ER16463 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER16490 inc_laborbus_ref	// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER16465 inc_labor_sp		// spouse, labor income (excluding farm and business)
ren ER16511 inc_laborbus_sp		// spouse, labor income from business 

// further variables adjustments
drop S4* ER*

gen wave = 1999

save "$psidwkddata/file1999adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1997 ////////////////////////
///////////////////////////////////////////////////////////

// in 1997, no wealth data was collected 
use "$psidwkddata/file1997.dta", clear

rename ER10005G famid68		// 1968 family identifier
rename ER12223R hhid		// household id number 
rename ER10002 famid		// 1997 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER12084 famweight 		// core family weight (longitudinal)

// demographic variables
ren ER10008 hhmemb			// number of members in family unit

ren ER10009 ageref			// age of reference person
ren ER10011 agesp			// age of spouse

ren ER10010 sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER10012 numchild 		// number of children in family unit

ren ER10016 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER12223A martgen		// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER12222 educref			// completed years of education (coded as actual number), reference person
ren ER12223 educsp			// completed years of education (coded as actual number), spouse


// income variables 
*total 
ren ER12079 totfaminc		// total family income
ren ER12077 inc_ss_all		// total family income from social security

*other family members
ren ER12073 inc_taxable_oth	// taxable income of other members in family unit
gen inc_asset_oth  = .		// total asset income of other members in family unit
gen inc_labor_oth  = .		// total labor income of other members in family unit
ren ER12075 inc_transf_oth	// total transfer income of other members in family unit (except social security)
gen inc_ss_oth = .			// total social security income of other members in family unit

*reference person + spouse
ren ER12069 inc_taxable_refsp	// taxable income of reference person and spouse  	
ren ER12071 inc_transf_refsp	// transfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income 
gen inc_rent_ref = .			// reference person, rental income 
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, income from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren ER12080  inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER12193 inc_laborbus_ref	// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER12082 inc_labor_sp		// spouse, labor income (excluding farm and business)
ren ER12214 inc_laborbus_sp		// spouse, labor income from business 


// further variables adjustments
drop ER*

gen wave = 1997

save "$psidwkddata/file1997adj.dta", replace

********************************************************
******* before 1997, frequency of PSID is annual *******
********************************************************

///////////////////////////////////////////////////////////
//////////////////////// wave 1996 ////////////////////////
///////////////////////////////////////////////////////////

// in 1996, no wealth data was collected 
use "$psidwkddata/file1996.dta", clear 

rename ER7005G famid68			// 1968 family identifier
rename ER9250R hhid				// household id number 
rename ER7002 famid				// 1996 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER9251 famweight 			// core family weight (longitudinal)

// demographic variables
ren ER7005 hhmemb				// number of members in family unit

ren ER7006 ageref				// age of reference person
ren ER7008 agesp				// age of spouse

ren ER7007 sexref				// sex of reference person
gen sexsp = . 					// before 2015: male was automatically head, spouse female

ren ER7009 numchild 			// number of children in family unit

ren ER7013 martref				// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER9250A martgen				// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER9249 educref				// completed years of education (coded as actual number), reference person
ren ER9250 educsp				// completed years of education (coded as actual number), spouse


// income variables
*total 
ren ER9244 totfaminc			//  total family income 
ren ER9243 inc_ss_all			//  total family income from social security

*other family members
ren ER9241 inc_taxable_oth		// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER9242 inc_transf_oth		// total transfer income of other members in family unit (except social security)
gen inc_ss_oth = .				// total social security income of other members in family unit 

*reference person + spouse
ren ER9237 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER9238 inc_transf_refsp		// tranfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income 
gen inc_rent_ref = .			// reference person, rental income 
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren ER9231 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER9210 inc_laborbus_ref		// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER9235 inc_labor_sp			// spouse, labor income (excluding farm and business)
ren ER9232 inc_laborbus_sp		// spouse, labor income from business 

// further variables adjustments
drop ER*

gen wave = 1996

save "$psidwkddata/file1996adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1995 ////////////////////////
///////////////////////////////////////////////////////////

// in 1995, no wealth data was collected 
use "$psidwkddata/file1995.dta", clear 

rename ER5005G famid68			// 1968 family identifier
rename ER6999R hhid				// household id number
rename ER5002 famid				// 2017 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER7000 famweight 			// core family weight (longitudinal)

// demographic variables 
ren ER5005 hhmemb				// number of members in family unit

ren ER5006 ageref				// age of reference person
ren ER5008 agesp				// age of spouse

ren ER5007 sexref				// sex of reference person
gen sexsp = . 					// before 2015: male was automatically head, spouse female

ren ER5009 numchild 			// number of children in family unit
ren ER5013 martref				// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER6999A martgen				// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER6998 educref				// completed years of education (coded as actual number), reference person
ren ER6999  educsp				// completed years of education (coded as actual number), SPOUSE


// income variables 
*total 
ren ER6993 totfaminc			//  total family income 
ren ER6992 inc_ss_all			//  total family income from social security

*other family members
ren ER6990 inc_taxable_oth		// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER6991 inc_transf_oth		// total transfer income of other members in family unit (except social security)
gen inc_ss_oth = .				// total social security income of other members in family unit

*reference person + spouse
ren ER6986 inc_taxable_refsp		// taxable income of reference person and spouse
ren ER6987 inc_transf_refsp			// tranfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .					// reference person, social security income 
gen inc_rent_ref = .				// reference person, rental income
gen inc_div_ref = .					// reference person, income from dividends
gen inc_interest_ref = .			// reference person, income from interest
gen inc_trustfund_ref = .			// reference person, income from trusts/rolyalties

ren ER6980 inc_labor_ref			// reference person, labor income (excluding farm and business)
ren ER6959 inc_laborbus_ref			// reference person, labor income from business

*spouse
gen inc_ss_sp = .					// spouse, social security income
gen inc_rent_sp = .					// spouse, rental income
gen inc_div_sp = .					// spouse, income from dividends
gen inc_interest_sp = .				// spouse, income from interest
gen inc_trustfund_sp = .			// spouse, income from trusts/rolyalties
ren ER6984 inc_labor_sp				// spouse, labor income (excluding farm and business)
ren ER6981 inc_laborbus_sp			// spouse, labor income from business


// further variables adjustments
drop ER*

gen wave = 1995

save "$psidwkddata/file1995adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1994 ////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth1994.dta", clear
ren S301 famid
save "$psidwkddata/wealth1994.dta", replace
restore

use "$psidwkddata/file1994.dta", clear

rename ER2005G famid68		// 1968 family identifier
rename ER4159R hhid			// household id number 
rename ER2002 famid			// 1994 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren ER4160 famweight 		// core family weight (longitudinal)

// merge with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth1994.dta"
drop if _merge == 1
drop _merge // NOTE: some form master not matched

// demographic variables 
ren ER2006 hhmemb			// number of members in family unit

ren ER2007 ageref			// age of reference person
ren ER2009 agesp			// age of spouse

ren ER2008	sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren ER2010 numchild			// number of children in family unit

ren ER2014 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren ER4159A martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren ER4158 educref			// completed years of education (coded as actual number), reference person
ren ER4159 educsp			// completed years of education (coded as actual number), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S320	homequ				// home equity (value of home minus mortgage) 
ren S316	wealthwoequity		// wealth without equity (sum of all assets net of debt)
ren S317	wealth				// wealth including equity (sum of all assets net of debt, plus home equity)
ren S310	stocks				// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S311	amountstock			// amount of stock holdings
ren S304 	savings				// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S305	amtsavings			// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S309

// income variables 
*total 
ren ER4153 totfaminc			//  total family income 
ren  ER4152 inc_ss_all 			//  total family income from social security

*other family members
ren ER4150 inc_taxable_oth		// taxable income of other members in family unit
gen inc_asset_oth  = .			// total asset income of other members in family unit
gen inc_labor_oth  = .			// total labor income of other members in family unit
ren ER4151 inc_transf_oth		// total transfer income of other members in family unit (except social security)
gen inc_ss_oth = .				// total social security income of other members in family unit 

*respondent + spouse
ren ER4146 inc_taxable_refsp	// taxable income of reference person and spouse
ren ER4147 inc_transf_refsp		// transfer income of reference person and spouse

*reference person  
* for final defintion of income: work with labor income and transfer income of ref and spouse
gen inc_ss_ref = .				// reference person, social security income
gen inc_rent_ref = .			// reference person, rental income
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren ER4140 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren ER4119 inc_laborbus_ref		// reference person, labor income from business

*spouse
gen inc_ss_sp = .				// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren ER4144 inc_labor_sp			// spouse, labor income (excluding farm and business)
ren ER4141 inc_laborbus_sp		// spouse, labor income from business 

// further variables adjustments
drop S3* ER*

gen wave = 1994

save "$psidwkddata/file1994adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1993 ////////////////////////
///////////////////////////////////////////////////////////

// in 1993, no wealth data was collected 
use "$psidwkddata/file1993.dta", clear

rename V22400 famid68		// 1968 family identifier
rename V23356 hhid			// household id number 
rename V21602 famid			// 1993 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren V23361 famweight 		// core family weight (longitudinal)

// demographic variables 
ren V22405 hhmemb			// number of members in family unit

ren V22406 ageref			// age of reference person
ren V22408 agesp			// age of spouse

ren V22407	sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren V22409 numchild			// number of children in family unit

ren V22412  martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren V23336 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren V23333 educref 			// completed years of education (coded as actual number), reference person
ren V23334 educsp			// completed years of education (coded as actual number), spouse

// income variables 
*total 
ren V23322 totfaminc		//  total family income
gen inc_ss_all = .			//  total family income from social security (not collected)

*other family members
ren V22373 inc_taxable_oth	// taxable income of other members in family unit
ren V22371  inc_asset_oth	// total asset income of other members in family unit
ren V22367 inc_labor_oth	// total labor income of other members in family unit
ren V22396 inc_transf_oth	// total transfer income of other members in family unit (except social security)
ren V22380 inc_ss_oth		// total social security income of other members in family unit 

*respondent + spouse			
ren V21959 inc_taxable_refsp	// taxable income of reference person and spouse
ren V22366 inc_transf_refsp		// tranfer income of reference person and spouse

*respondent
ren V22027 inc_ss_ref 			// reference person, social security income 
ren V21829 inc_rent_ref			// reference person, rental income 
ren V21845 inc_div_ref			// reference person, income from dividends
ren V21861 inc_interest_ref		// reference person, inocme from interest
ren V21877 inc_trustfund_ref	// reference person, income from trusts/rolyalties
ren V23323 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren V21738 inc_laborbus_ref		// reference person, labor income from business

*spouse
ren V22301 inc_ss_sp 			// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
ren V21909 inc_div_sp			// spouse, income from dividends
ren V21925 inc_interest_sp		// spouse, income from interest
ren V21941 inc_trustfund_sp		// spouse, income from trusts/rolyalties
ren V21807 inc_labor_sp			// spouse, labor income (excluding farm and business)
ren V21806 inc_laborbus_sp		// spouse, labor income from business 


// further variables adjustments
drop V*

gen wave = 1993

save "$psidwkddata/file1993adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1992 ////////////////////////
///////////////////////////////////////////////////////////

// in 1992, no wealth data was collected 
use "$psidwkddata/file1992.dta", clear

rename V20621 famid68		// 1968 family identifier
rename V21542 hhid			// household id number
rename V20302 famid			// 1992 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren V21547 famweight 		// core family weight (longitudinal)

 // demographic variables
ren V20650 hhmemb			// number of members in family unit

ren V20651 ageref			// age of reference person
ren V20653 agesp			// age of spouse

ren V20652 	sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren V20654 numchild			// number of children in family unit

ren V20657  martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren V21522 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren V21504 educref			// completed years of education (coded as actual number), reference person 
ren V21505 educsp			// completed years of education (coded as actual number), spouse

// income variables 
*total 
ren V21481 totfaminc		//  total family income 
gen inc_ss_all = .			//  total family income from social security (not collected)

*other family members
ren V20601 inc_taxable_oth	// taxable income of other members in family unit
ren V20603 inc_asset_oth	// total asset income of other members in family unit
gen inc_labor_oth = .		// total labor income of other members in family unit
ren V20616 inc_transf_oth	// total transfer income of other members in family unit (except social security)
ren V20608 inc_ss_oth		// total social security income of other members in family unit

*reference person  + spouse
ren V20451 inc_taxable_refsp	// taxable income of reference person and spouse
ren V20501 inc_transf_refsp		// tranfer income of reference person and spouse

*reference person 
* for final defintion of income: work with labor income and transfer income of ref and spouse
ren V20465 inc_ss_ref 			// reference person, social security income 
ren V20444 inc_rent_ref			// reference person, rental income 
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren V21484 inc_labor_ref		// reference person, labor income (excluding farm and business)
ren V20428 inc_laborbus_ref		// reference person, labor income from business

*spouse
ren V20486 inc_ss_sp 			// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren V20436 inc_labor_sp         // from 1992: labor income spouse includes business + farm income 


// further variables adjustments
drop V*

gen wave = 1992

save "$psidwkddata/file1992adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1991 ////////////////////////
///////////////////////////////////////////////////////////

// in 1991, no wealth data was collected
use "$psidwkddata/file1991.dta", clear

rename V19321 famid68		// 1968 family identifier
rename V20236 hhid			// household id number
rename V19002 famid			// 1991 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren V20243 famweight 		// core family weight (longitudinal)

// demographic variables  
ren V19348 hhmemb			// number of members in family unit

ren V19349 ageref			// age of reference person
ren V19351 agesp			// age of spouse

ren V19350 	sexref			// sex of reference person
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren V19352 numchild			// number of children in family unit
ren V19355 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren V20216 martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren V20198 educref 			// completed years of education (coded as actual number), reference person
ren V20199 educsp			// completed years of education (coded as actual number), spouse

// income variables
*total 
ren V20175 totfaminc			//  total family income 
gen inc_ss_all = .				//  total family income from social security (not collected)

*other family members
ren V19301 inc_taxable_oth		// taxable income of other members in family unit
ren V19303 inc_asset_oth		// total asset income of other members in family unit
gen inc_labor_oth = .			// total labor income of other members in family unit
ren V19316 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren V19308 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren V19151 inc_taxable_refsp	// taxable income of reference person and spouse
ren V19201 inc_transf_refsp		// tranfer income of reference person and spouse

*reference person
ren V19165 inc_ss_ref 			// reference person, social security income 
ren V19144 inc_rent_ref			// reference person, rental income 
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, income from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren V20178 inc_labor_ref  		// reference person, labor income (excluding farm and business)
ren V19128 inc_laborbus_ref		// reference person, labor income from business

*spouse
ren V19186 inc_ss_sp 			// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren V19136 inc_labor_sp  		// from 1992: labor income spouse includes business + farm income 


// further variables adjustments
drop V*

gen wave = 1991

save "$psidwkddata/file1991adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1990/////////////////////////
///////////////////////////////////////////////////////////

// in 1990, no wealth data was collected
use "$psidwkddata/file1990.dta", clear

rename V18021 famid68		// 1968 family identifier
rename V18936 hhid			// household id number
rename V17702 famid			// 1990 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren V18943 famweight 		// core family weight (longitudinal)

// demographic variables 
ren V18048 hhmemb			// number of members in family unit

ren V18049 ageref			// age of reference person
ren V18051 agesp			// age of spouse

ren V18050 	sexref			// sex of reference person 
gen sexsp = . 				// before 2015: male was automatically head, spouse female

ren V18052 numchild			// number of children in family unit

ren V18055 martref			// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren V18916  martgen			// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren V18898 educref			// completed years of education (coded as category), reference person
ren V18899 educsp 			// completed years of education (coded as category), spouse

// income variables 
*total 
ren V18875 totfaminc			//  total family income 
gen inc_ss_all = .				//  total family income from social security (not collected)

*other family members
ren V18001 inc_taxable_oth		// taxable income of other members in family unit
ren V18003 inc_asset_oth		// total asset income of other members in family unit
gen inc_labor_oth = .			// total labor income of other members in family unit
ren V18016 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren V18008 inc_ss_oth			// total social security income of other members in family unit 

*reference person + spouse
ren V17851 inc_taxable_refsp	// taxable income of reference person and spouse
ren V17901 inc_transf_refsp		// transfer income of reference person and spouse

*reference person
ren V17865 inc_ss_ref 			// reference person, social security income 
ren V17844 inc_rent_ref			// reference person, rental income
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren V18878 inc_labor_ref  		// reference person, labor income (excluding farm and business)
ren V17828 inc_laborbus_ref		// reference person, labor income from business

*spouse
ren V17886 inc_ss_sp 			// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren V17836 inc_labor_sp  		// from 1992: labor income spouse includes business + farm income 
	
// further variables adjustments
drop V*

gen wave = 1990

save "$psidwkddata/file1990adj.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1989/////////////////////////
///////////////////////////////////////////////////////////

// up until 2007, wealth is collected in individual file
preserve
use "$psidwkddata/wealth1989.dta", clear
ren S201 famid
save "$psidwkddata/wealth1989.dta", replace
restore

use "$psidwkddata/file1989.dta", clear

rename V16605 famid68		// 1968 family identifier
rename V17584 hhid			// household id number 
rename V16302 famid			// 1989 family interview ID number
* note: famid is NOT consistent across years (need indiv. file to identify panel structure)

ren V17612 famweight 		// core family weight (longitudinal)

*merge with family data (on family id)
merge 1:1 famid using "$psidwkddata/wealth1989.dta"
drop _merge // all matched 

// demographic variables 
ren V16630 hhmemb		// number of members in family unit

ren V16631 ageref		// age of reference person
ren V16633 agesp		// age of spouse

ren V16632	sexref		// sex of reference person
gen sexsp = . 			// before 2015: male was automatically head, spouse female

ren V16634 numchild		// number of children in family unit

ren V16637 martref		// marital status of reference person (1: married, 2: never married, 3: widowed, 4: divorced, 5: separated, 8,9: no answer)
ren V17565 martgen		// generated marital status (1: married or cohabiting, 2: single, 3: widowed and no partner present, 4: divored and no partner present, 5: separated and no partner present)

ren V17545 educref 		// completed years of education (coded as category), reference person
ren V17546 educsp		// completed years of education (coded as category), spouse

// wealth variables: for all variables, work with variables for which missing values are imputed by PSID
ren S220	homequ			// home equity (value of home minus mortgage) 
ren S216	wealthwoequity	// wealth without equity (sum of all assets net of debt)
ren S217	wealth			// wealth including equity (sum of all assets net of debt, plus home equity)
ren S210	stocks			// do you hold stocks/mutual funds/investment trusts (excl. pensions, IRAs) yes/no
ren S211	amountstock		// amount of stock holdings
ren S204 	savings			// do you have checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs) yes/no
ren S205	amtsavings		// amount in: checking/savings accounts, moneny market accoutns, cds, government bonds, treasury bills (excl. pensions, IRAs)

* financial wealth: wealth without equity minus vlaue of other real estate (value if sold and all debts paid off)
gen finwealth = wealthwoequity - S209

// income variables 
*total 
ren V17533 totfaminc			//  total family income 
gen inc_ss_all = .				//  total family income from social security (not collected)

*other family members
ren V16585 inc_taxable_oth		// taxable income of other members in family unit
ren V16587 inc_asset_oth		// total asset income of other members in family unit
gen inc_labor_oth = .			// total labor income of other members in family unit
ren V16600 inc_transf_oth		// total transfer income of other members in family unit (except social security)
ren V16592 inc_ss_oth			// total social security income of other members in family unit

*reference person + spouse
ren V16435 inc_taxable_refsp	// taxable income of reference person and spouse
ren V16485 inc_transf_refsp		// tranfer income of reference person and spouse

*reference person 
ren V16449 inc_ss_ref 			// reference person, social security income 
ren V16428 inc_rent_ref			// reference person, rental income
gen inc_div_ref = .				// reference person, income from dividends
gen inc_interest_ref = .		// reference person, inocme from interest
gen inc_trustfund_ref = .		// reference person, income from trusts/rolyalties
ren V17534 inc_labor_ref  		// reference person, labor income (excluding farm and business)
ren V16412 inc_laborbus_ref		// reference person, labor income from business

*spouse
ren V16470 inc_ss_sp 			// spouse, social security income
gen inc_rent_sp = .				// spouse, rental income 
gen inc_div_sp = .				// spouse, income from dividends
gen inc_interest_sp = .			// spouse, income from interest
gen inc_trustfund_sp = .		// spouse, income from trusts/rolyalties
ren V16420 inc_labor_sp   		// from 1992: labor income spouse includes business + farm income 


// further variables adjustments
drop S2* V*

gen wave = 1989

save "$psidwkddata/file1989adj.dta", replace


////////////////////////////////////////////////////////////////////
////////////////COMBINE ADJUSTED INDIVIDUAL WAVES///////////////////
////////////////////////////////////////////////////////////////////

global varlist famid* hhid hhmemb age* sex* numchild mart* ///
totfaminc homequ wealth* stocks amountstock  wave ///
 savings amtsavings educ* finwealth inc* famweight

global varlist_noasset famid* hhid hhmemb age* sex* numchild mart* ///
totfaminc wave educ* inc*  famweight

use "$psidwkddata/file2017adj.dta", clear
keep $varlist

// check for duplicates on identifier (famid)
bysort famid: gen dup = _N
tab dup
drop dup // no duplicates


// combine datasets across years
foreach num of numlist 2015(-2)1999 {
append using "$psidwkddata/file`num'adj.dta", keep($varlist)
}

foreach num of numlist 1994(-5)1989 {
append using "$psidwkddata/file`num'adj.dta", keep($varlist)
}

foreach num of numlist 1997(-1)1995 {
append using "$psidwkddata/file`num'adj.dta", keep($varlist_noasset)
}

foreach num of numlist 1993(-1)1990 {
append using "$psidwkddata/file`num'adj.dta", keep($varlist_noasset)
}

// save the combined dataset
sort wave
save "$psidwkddata/fam_combined.dta", replace



